package com.rao.guli.service.edu.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rao.guli.service.base.result.R;
import com.rao.guli.service.edu.entity.Teacher;
import com.rao.guli.service.edu.entity.query.TeacherQuery;
import com.rao.guli.service.edu.feign.OssFileService;
import com.rao.guli.service.edu.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author rao
 * @since 2021-07-02
 */
// 跨域
@CrossOrigin
@RestController
@RequestMapping("/admin/edu/teacher")
@Api(tags = "讲师管理")
@Slf4j
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private OssFileService ossFileService;

    @ApiOperation("所有讲师列表")
    @GetMapping("/list")
    public R listAll() {
//        log.warn("这是一个警告信息");
//        log.error("这是一个错误信息");
        List<Teacher> list = teacherService.list();
        return R.ok().data("items", list).message("获取讲师列表成功");
    }

    @ApiOperation("根据 id 删除讲师")
    @DeleteMapping("/remove/{id}")
    public R removeById(@ApiParam(value = "讲师 Id", required = true) @PathVariable String id) {
        // 删除讲师头像（头像不影响主业务，故而失败也可以继续向下执行）
        boolean avatar = teacherService.removeAvatarById(id);
        if (!avatar){
            log.warn("讲师头像删除失败，讲师 id" + id);
        }
        // 删除讲师信息
        boolean result = teacherService.removeById(id);
        if (result) {
            return R.ok().message("删除讲师成功");
        } else {
            return R.error().message("删除讲师失败");
        }
    }

    @ApiOperation("批量删除讲师")
    @DeleteMapping("/batch-remove")
    public R removeRows(@ApiParam(value = "讲师 id 列表", required = true) @RequestBody List<String> idList) {
        boolean result = teacherService.removeByIds(idList);
        if (result) {
            return R.ok().message("批量删除讲师成功");
        } else {
            return R.error().message("批量删除讲师失败");
        }
    }

    @ApiOperation("讲师列表（分页）")
    @GetMapping("/list/{page}/{limit}")
    public R listPage(@ApiParam(value = "当前页码", required = true) @PathVariable Long page, @ApiParam(value = "每页记录条数", required = true
    ) @PathVariable Long limit) {

        Page<Teacher> pageModel = new Page<>(page, limit);

        teacherService.page(pageModel);

        return R.ok().data("pageModel", pageModel);
    }

    @ApiOperation("教师条件查询列表（分页）")
    @GetMapping("/condition/{page}/{limit}")
    public R listByCondition(@ApiParam(value = "当前页码", required = true) @PathVariable Long page, @ApiParam(value = "每页记录条数", required = true
    ) @PathVariable Long limit, @ApiParam("教师查询条件") TeacherQuery teacherQuery) {

        IPage<Teacher> pageModel = teacherService.selectPage(page, limit, teacherQuery);

        return R.ok().data("pageModel", pageModel);
    }

    @ApiOperation("添加教师")
    @PostMapping("/save")
    public R save(@ApiParam(value = "教师对象", required = true) @RequestBody Teacher teacher) {
        boolean save = teacherService.save(teacher);
        if (save) {
            return R.ok().message("添加成功");
        } else {
            return R.error().message("添加失败");
        }
    }

    @ApiParam("更新教师信息")
    @PutMapping("/update")
    public R update(@ApiParam(value = "教师对象", required = true) @RequestBody Teacher teacher) {
        boolean update = teacherService.updateById(teacher);
        if (update) {
            return R.ok().message("教师信息更新成功");
        } else {
            return R.error().message("教师信息更新失败");
        }
    }

    @ApiParam("根据 id 查询教师信息")
    @GetMapping("/get/{id}")
    public R getById(@ApiParam(value = "教师对象", required = true) @PathVariable String id) {
        Teacher teacher = teacherService.getById(id);
        if (teacher != null) {
            return R.ok().data("item", teacher);
        } else {
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation("根据左关键字查询讲师列表")
    @GetMapping("/list/name/{key}")
    public R selectNameListByKey(
            @ApiParam(value = "查询关键字", required = true)
            @PathVariable String key) {
        List<Map<String, Object>> nameList = teacherService.selectNameListByKey(key);

        return R.ok().data("nameList", nameList);
    }

    @GetMapping("/test")
    @ApiOperation("测试服务调用")
    public R test(){
        ossFileService.test();
        return R.ok();
    }
}

